package com.api.gbhg.liuwenbo.version2.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.version2.pojo.BudgetSectionIncome2;
import weaver.conn.RecordSet;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.time.LocalDate;
import java.util.*;

/**
 * @Description 非财政拨款查询
 * @Author lwb
 * @Date 2022.8.3
 */
@Path("gb/NonFinance")
public class NonFinanceApi{

    @POST
    @Path("/getNonFinance")
    @Produces(MediaType.APPLICATION_JSON)
    public String getNonFinance(Map param){
        Map msgMap = new HashMap<>();

        if (param == null ||!param.containsKey("nf") || !param.containsKey("type") || !param.containsKey("dw")){
            msgMap.put("code","500");
            msgMap.put("msg","缺少参数 请检查参数是否完整！");
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }

        param.put("jz","yqndyjjzje");
        Integer nf = param.get("nf").equals("")? LocalDate.now().getYear():Integer.parseInt(param.get("nf").toString());
        param.put("nf",nf);

        //单位条件
        String dwid = "";
        boolean dwCondition = false;
        if ("xzdw".equals(param.get("type"))){
            //行政单位
            dwCondition = true;
            Object dw = param.get("dw");
            if ("".equals(dw)){
                dwid = AdministrationApi.getdw();
            }else{
                dwid = AdministrationApi.getdw(dw.toString());
            }
        }else if("sydw".equals(param.get("type"))){
            //事业单位
            dwCondition = true;
            Object dw = param.get("dw");
            if ("".equals(dw)){
                dwid = CauseApi.getdw();
            }else{
                dwid = CauseApi.getdw(dw.toString());
            }
        }

        //返回结果集
        Map<String,List<BudgetSectionIncome2>> resultMap = new HashMap<>();
        try{
            Map<Integer,String> fzyDwIdName = getFzyDwIdName(dwCondition,dwid);
            Set<Map.Entry<Integer, String>> entries = fzyDwIdName.entrySet();
            Iterator<Map.Entry<Integer, String>> iterator = entries.iterator();
            while (iterator.hasNext()){
                Map.Entry<Integer, String> entry = iterator.next();
                param.put("dw",entry.getKey());
                param.put("dwName",entry.getValue());
                List<BudgetSectionIncome2> fczbksrData = new BudgetSectionIncomeApi2().getFczbksrData(param);
                resultMap.put(entry.getValue()+"_"+entry.getKey(),fczbksrData);
            }

            msgMap.put("code","200");
            msgMap.put("msg","");
            msgMap.put("data",resultMap);
            return JSON.toJSONString(msgMap);
        }catch (Exception e){
            e.printStackTrace();
            msgMap.put("code","500");
            msgMap.put("msg","500 程序异常,请联系管理员！"+e.getMessage());
            return JSON.toJSONString(msgMap);
        }
    }

    /**
     * 获取非财政拨款单位id、名称
     * @return
     */
    public Map<Integer,String> getFzyDwIdName(boolean Condition,String dwid){
        RecordSet rs = new RecordSet();
        String sql = "select id,subcompanyname from HrmSubCompany where id in(select dw from uf_fzysr group by dw)";
        if (Condition == true){
            if (dwid.isEmpty())
                sql += " and id in (0) ";
            else
                sql += " and id in ("+dwid+") ";
        }
        rs.executeQuery(sql);
        Map<Integer,String> map = new HashMap<>();
        while (rs.next()) {
            map.put(rs.getInt("id"),rs.getString("subcompanyname"));
        }
        return map;
    }

}
